package cn.bjit.service.impl;

import cn.bjit.interceptor.LoginInterceptor;
import cn.bjit.mapper.AddressMapper;
import cn.bjit.model.LoginUser;
import cn.bjit.model.AddressDO;
import cn.bjit.request.AddressAddReqeust;
import cn.bjit.service.AddressService;
import cn.bjit.vo.AddressVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {


    @Resource
    private AddressMapper addressMapper;

    /**
     * 新增收货地址
     * @param addressAddReqeust
     */
    @Override
    public void add(AddressAddReqeust addressAddReqeust) {
        LoginUser loginUser= LoginInterceptor.threadLocal.get();
        AddressDO addressDO=new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());

        BeanUtils.copyProperties(addressAddReqeust,addressDO);

        //是否有默认的收货地址
        if(addressDO.getDefaultStatus()==1){
            //有就查到默认地址
            AddressDO defaultAddressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()).eq("default_status", 1));
            if(defaultAddressDO!=null){
                defaultAddressDO.setDefaultStatus(0);
                addressMapper.update(defaultAddressDO,new QueryWrapper<AddressDO>().eq("id",defaultAddressDO.getId()));
            }
        }
       int row= addressMapper.insert(addressDO);
        log.info("row={},data={}",row,addressDO);
    }

    /**
     * 根据id查找地址详情
     * @param id
     * @return
     */
    @Override
    public AddressVO findById(long id) {
        LoginUser loginUser= LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id).eq("user_id",loginUser.getId()));
        if(addressDO==null){
            return null;
        }
        AddressVO  addressVO=new AddressVO();
        addressVO.setUserId(loginUser.getId());
        BeanUtils.copyProperties(addressDO,addressVO);
        return  addressVO;
    }

    /**
     * 根据id删除指定的收货地址
     * @param addressId
     * @return
     */
    @Override
    public int del(long addressId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", addressId).eq("user_id",loginUser.getId()));
        AddressVO addressVO=new AddressVO();
        addressVO.setId(addressId);
        BeanUtils.copyProperties(addressDO,addressVO);

        int rows = addressMapper.delete(new QueryWrapper<AddressDO>().eq("id",addressVO.getId()));
        return rows;
    }

    /**
     * 查询所有的收货地址
     * @return
     */
    @Override
    public List<AddressVO> findUserAllAddress() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id",loginUser.getId()));

        List<AddressVO> AddressVOList=new ArrayList<>();
        for (int i = 0; i <addressDOList.size() ; i++) {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressDOList.get(i),addressVO);
            AddressVOList.add(i,addressVO);
        }
        return AddressVOList;
    }
}
